<template>
	<view>
		<view class="example">
			<!-- 基础用法，不包含校验规则 -->
			<uni-forms ref="baseForm" :modelValue="baseFormData" :rules="rules">
				<uni-forms-item label="姓名" required name="name">
					<uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" />
				</uni-forms-item>

				<uni-forms-item label="性别" required name="sex">
					<uni-data-checkbox v-model="baseFormData.sex" :localdata="sexs" />
				</uni-forms-item>
				<uni-forms-item label="民族" required name="nation">
					<uni-easyinput v-model="baseFormData.nation" placeholder="请输入民族" />
				</uni-forms-item>
				<uni-forms-item label="日期时间" name="birthday">
					<uni-datetime-picker type="date" v-model="baseFormData.birthday" />
				</uni-forms-item>
				<uni-forms-item label="住址" required name="address">
					<uni-easyinput v-model="baseFormData.address" placeholder="请输入住址" />
				</uni-forms-item>
				<uni-forms-item label="身份证号码" required name="idCard">
					<uni-easyinput v-model="baseFormData.idCard" placeholder="请输入身份证号码" />
				</uni-forms-item>
				<uni-forms-item label="身份证正面照片" name="front">
					<uni-file-picker v-model="front" fileMediatype="image" :limit="1" mode="grid"
						@select="frontUpload" @delete="frontRemove" />
				</uni-forms-item>
				<uni-forms-item label="身份证反面照片" name="opposite">
					<uni-file-picker v-model="opposite" fileMediatype="image" :limit="1" mode="grid"
						@select="oppositeUpload" @delete="oppositeRemove" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit('baseForm')">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseFormData: {
					uid: uni.getStorageSync('userInfo').id,
					name: '',
					sex: 2,
					nation: '',
					birthday: '',
					address: '',
					idCard: '',
					front: '',
					opposite: ''
				},
				front: {},
				opposite: {},
				sexs: [{
					text: '男',
					value: 0
				}, {
					text: '女',
					value: 1
				}, {
					text: '保密',
					value: 2
				}],
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					sex: {
						rules: [{
							required: true,
							errorMessage: '请选择您的性别'
						}]
					},
					nation: {
						rules: [{
							required: true,
							errorMessage: '所属民族不能为空'
						}]
					},
					birthday: {
						rules: [{
							required: true,
							errorMessage: '出生日期不能为空'
						}]
					},
					address: {
						rules: [{
							required: true,
							errorMessage: '住址不能为空'
						}]
					},
					address: {
						rules: [{
							required: true,
							errorMessage: '身份证号码不能为空'
						}]
					},
					front: {
						rules: [{
							required: true,
							errorMessage: '请上传身份证正面照片'
						}]
					},
					opposite: {
						rules: [{
							required: true,
							errorMessage: '请上传身份证反面照片'
						}]
					},
				}
			}
		},
		methods: {
			// 移除身份证正面
			frontRemove(e) {
				this.front = {}
			},
			// 上传身份证正面
			async frontUpload(e) {
				this.front.url = await this.uploadFile(e.tempFilePaths[0])
				this.baseFormData.front = this.front.url
				console.log(this.front);
			},
			// 移除身份证反面
			oppositeRemove(e) {
				this.opposite = {}
			},
			// 上传身份证反面
			async oppositeUpload(e) {
				this.opposite.url = await this.uploadFile(e.tempFilePaths[0])
				this.baseFormData.opposite = this.opposite.url
				console.log(this.opposite);
			},
			// 文件上传
			uploadFile(imgUrl) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: 'http://localhost:8080/file/upload',
						header: {
							'Authorization': 'Bearer ' + wx.getStorageSync('token')
						},
						filePath: imgUrl,
						name: 'file',
						success: (resp) => {
							if (resp.statusCode == 200) {
								resolve(JSON.parse(resp.data).data);
							} else {
								reject(new Error('Upload failed'));
							}
						},
					})
				})
			},
			submit(ref) {
				let that = this
				this.$refs[ref].validate().then(res => {
					uni.request({
						url: 'http://localhost:8080/user/identityVerification',
						header: {
							'Authorization': 'Bearer ' + wx.getStorageSync('token')
						},
						method: 'POST',
						data: this.baseFormData,
						success(res) {
							if(res.data.code == 200){
								uni.setStorageSync('identity',that.baseFormData)
								uni.switchTab({
									url: '/pages/my/my'
								})
							}
						}
					})
				}).catch(err => {
					console.log('err', err);
				})
			}
		},
		onLoad(){
			
		}
	}
</script>

<style>
	.example {
		padding: 15px;
		background-color: #fff;
	}
</style>